package com.xgo.http;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.untils.Common;

public class Dahewang {
	private static int index = 1;
	private static int max = 52;
	private static String path = "d:/玉雕精品/"; 
	private static String baseLink = "http://photo.dahe.cn/2011/05-09/100705063_"; 
	private static Logger logger = LoggerFactory.getLogger(XinxinCrawler.class);
	public static void main(String[] args) {
		Dahewang crawler = new Dahewang();
		crawler.run(baseLink + index + ".html");
	}
	
	public void run(String szUrl){
		if(index <= max){
			//下载页面
			Document doc = null;
			try {
				System.out.println(szUrl);
				URL url = new URL(szUrl);
				doc = Jsoup.parse(url, 60000);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(doc == null ){
				logger.error(" muju html down load failed.");
				return;
			}
			try {
				parseShop(doc);
			} catch (IOException e1) {
				logger.error(e1.getMessage());
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			index++;
			run(baseLink + index  + ".html");
		
		}
	}

	private void parseShop(Document doc) throws IOException{
		//获取页面商品
		String url = doc.getElementsByAttributeValue("id", "imgBCon").get(0).getElementsByTag("img").attr("src");
				
		int fileFormatBegin = url.lastIndexOf(".");
		String fileFormat = url.substring(fileFormatBegin);
		try {
			Common.download(url, path + index + fileFormat);
		} catch (IOException e) {
			throw new IOException("download image error.:" + url);
		}
	}
}
